package cn.kgc.vue.controller;

import cn.kgc.vue.commons.KgcLogs;
import cn.kgc.vue.commons.KgcOperation;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2023/12/7
 * @description
 */
@RestController
@RequestMapping("roles")
@Api(tags = "角色模块")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;


    @ApiOperation("角色下拉选数据接口")
    @GetMapping("select")
    public ResponseResult roleSelect(){
        List<Role> list = roleService.list();
       return   ResponseResult.success().data(list);
    }


    @ApiOperation("角色列表")
    @PostMapping("list")
    public ResponseResult roleList(@RequestBody QueryCondition<Role> queryCondition){

        return  roleService.roleList(queryCondition);
    }


    @KgcLogs(model = "角色模块",operation = KgcOperation.UPDATE)
    @ApiOperation("添加/修改角色信息")
    @RequiresPermissions(value = {"sys:role:add","sys:role:update"},logical = Logical.OR)
    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdate(@RequestBody Role role){

        //  判定角色名是否存在
        String roleCh = role.getRoleCh();
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getRoleCh,roleCh);
        Role one = roleService.getOne(lambda);
        if (ObjectUtils.isNotNull(one)){
            return ResponseResult.fail().message("角色名已被占用");
        }
        // 角色标识是否存在
        String roleEn = role.getRoleEn();
        lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getRoleEn,roleEn);
        one = roleService.getOne(lambda);
        if (ObjectUtils.isNotNull(one)){
            return ResponseResult.fail().message("角色标识已被占用");
        }
        //  该方法会根据实体类中是否有id  来选择不同的操作  有id 执行更新  没有 执行添加
        roleService.saveOrUpdate(role);
        return  ResponseResult.success();
    }

    @ApiOperation("根据角色id查询角色信息")
    @GetMapping("{rid}")
    public ResponseResult getRoleById(@PathVariable Integer rid){

        Role role = roleService.getById(rid);
        return   ResponseResult.success().data(role);
    }



    @KgcLogs(model = "角色模块",operation = KgcOperation.DELETE)
    @ApiOperation("删除角色信息")
    @DeleteMapping("{rid}")
    @RequiresPermissions("sys:role:delete")
    public ResponseResult  deleteRoleById(@PathVariable Integer rid){

        return   roleService.removeRoleById(rid);
    }


    @ApiOperation("根据角色id查询权限信息")
    @GetMapping("permissions/{rid}")
    public ResponseResult  getPermissionsByRoleId(@PathVariable Integer rid){

        return   roleService.getPermissionsByRoleId(rid);
    }


    @KgcLogs(model = "角色模块",operation = KgcOperation.ADD)
    //addPermissions
    @ApiOperation("添加角色权限信息")
    @PostMapping("addPermissions")
    public ResponseResult  addPermissionsByRoleId(@RequestBody Map<String,Object> permissions){

        return roleService.addPermissionsByRoleId(permissions) ;
    }
}
